package com.jay.example.IndoorPositioningSystem;

/****
 * https://gitee.com/unw1900/Indoor_Positioning_System
 */
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

public class RealtimeUpdates extends Fragment {
    public static final String ARG_SECTION_NUMBER = "section_number";
    private final Handler mHandler = new Handler();
    private Runnable mTimer1;

    LineGraphSeries<DataPoint> mSeries1;
    LineGraphSeries<DataPoint> mSeries2;
    double accLastXValue = 5d;
    double maLastXValue = 5d;
    double degreeLastXValue = 5d;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.graphview, container, false);
        mSeries1 = new LineGraphSeries<>();
        mSeries2 = new LineGraphSeries<>();
        GraphView graph2 = (GraphView) rootView.findViewById(R.id.graph2);

//        graph2.getViewport().setXAxisBoundsManual(true);
//        graph2.getViewport().setMinX(-50);
//        graph2.getViewport().setMaxX(50);
//        graph2.getViewport().setYAxisBoundsManual(true);
//        graph2.getViewport().setMaxY(50);
//        graph2.getViewport().setMinY(-50);

        graph2.addSeries(mSeries1);
        graph2.addSeries(mSeries2);
        mSeries2.setColor(Color.RED);
        graph2.getViewport().setXAxisBoundsManual(true);
        graph2.getViewport().setMinX(0);
        graph2.getViewport().setMaxX(100);
        graph2.getViewport().setYAxisBoundsManual(true);
        graph2.getViewport().setMaxY(5);
        graph2.getViewport().setMinY(0);

        graph2.getGridLabelRenderer().setNumHorizontalLabels(5);
        graph2.getGridLabelRenderer().setNumVerticalLabels(5);
        graph2.getGridLabelRenderer().setVerticalAxisTitle("Acc（m/s²）");
        graph2.getGridLabelRenderer().setHorizontalAxisTitle("Time（0.1s）");

//        graph2.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.BOTH);
//        graph2.setTitle("设备线性加速度");
        graph2.setTitleColor(Color.BLUE);
        graph2.getViewport().setScalable(true);
        graph2.getViewport().setScalableY(true);


        return rootView;
    }

//    @SuppressWarnings("deprecation")
//    @Override
//    public void onAttach(Activity activity) {
//        super.onAttach(activity);
//        ((MainActivity) activity).onSectionAttached(
//                getArguments().getInt(MainActivity.ARG_SECTION_NUMBER));
//    }

    @Override
    public void onResume() {
        super.onResume();
        MainActivity activity = (MainActivity) getActivity();
//        mTimer1 = new Runnable() {
//            @Override
//            public void run() {
//                graph2LastXValue += 1d;
//                mSeries1.appendData(new DataPoint(graph2LastXValue, getdata()), true, 40);
//                mHandler.postDelayed(this, 200);
//            }
//        };
//        mHandler.postDelayed(mTimer1, 1000);
    }

    @Override
    public void onPause() {
        mHandler.removeCallbacks(mTimer1);
        super.onPause();
    }

    /****
     * 原始acc数据动态图形绘制
     */
//    public void getdata(long stepdata) {
//        getdata();
//    }

    /****
     * 原始acc数据动态图形绘制
     * @param accdata acc数据
     * @param Xvalue 时间戳
     */
    public void getdata(double accdata,long Xvalue){
        //accLastXValue += 0.2;//X轴步进
        accLastXValue = Xvalue*0.01;
//        if (accLastXValue <= 100) {
        mSeries1.appendData(new DataPoint(accLastXValue, accdata), true, 600);
//        }
    }

    /****
     * 获取滑动滤波器输出，并绘制曲线
     * @param filterdata 经滑动滤波器输出的数据
     */
    public void getfilterdata(double filterdata,long Xvalue) {
        //maLastXValue += 0.2;//X轴步进
        maLastXValue = Xvalue*0.01;
        mSeries2.appendData(new DataPoint(maLastXValue, filterdata), true, 600);
    }

    public void getDegree(double accdata,long Xvalue){
//        degreeLastXValue+= 0.2;//X轴步进
        double degreeLastXValue = Xvalue*0.01;
//        if (accLastXValue <= 100) {
        mSeries1.appendData(new DataPoint(degreeLastXValue, accdata), true, 600);

//        }
    }
}
//    private DataPoint[] generateData() {
//        int count = 30;
//        DataPoint[] values = new DataPoint[count];
//        for (int i=0; i<count; i++) {
//            double x = i;
//            double f = mRand.nextDouble()*0.15+0.3;
//            double y = Math.sin(i*f+2) + mRand.nextDouble()*0.3;
//            DataPoint v = new DataPoint(x, y);
//            values[i] = v;
//        }
//        return values;
//    }

//    double mLastRandom = 2;
//    Random mRand = new Random();
//    private double getRandom() {
//        return mLastRandom += mRand.nextDouble()*0.5 - 0.25;
//    }
//}
